﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;

public partial class MembershipCard_wechat_web_ProductsExchange : WeiPage
{
    PRODUCTBO pbo = new PRODUCTBO();
    PersonInfoBO fbo = new PersonInfoBO();
    ShoppingCartBO sbo = new ShoppingCartBO();
    public string id = "0";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["code"] != null)
            {
                try
                {
                    string url = string.Format(@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + GetAppid() + "&secret=d9bfd969e149c1844698cfd1f962bdad&code={0}&grant_type=authorization_code", Request.QueryString["code"].ToString());
                    string token = PostRequest(url);
                    if (token.Contains("7200"))
                    {
                        string[] b = token.Split('\"');
                        Session["FromUserName"] = b[13];
                        Session["ToUserName"] = "wx8ecea9aaa3d6162b";
                    }
                }
                catch (Exception)
                {

                }
            }
            if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
            {
                string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
                string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
                Session["FromUserName"] = user;
                Session["ToUserName"] = user2;
                PersonInfo per = fbo.GetModel(user);
                if (per != null)
                {
                    List<ShoppingCart> list = sbo.GetModelList(" pid=" + per.ID);
                    if (list.Count > 0)
                    {
                        sumcar.InnerHtml = "<span><a href='ShopCar.aspx'><em class='indexicon4'></em><i>" + list.Count + "</i></a></span>";
                    }
                }
            }
            if (Request.QueryString["id"] != null)
            {
                id = Request.QueryString["id"].ToString();
                PRODUCT p = pbo.GetModel(int.Parse(Request.QueryString["id"].ToString()));
                if (p != null)
                {
                    string str1 = @" <ul class='num'> 
                    <li class='on'></li>";
                    string str2 = @"<ul class='containercon'>";
                    if (p.PicUrl != "")
                    {
                        str2 += "<li><a href='#'><img src='" + p.PicUrl + "' width='320' height='320' /></a></li>";
                    }
                    if (p.PicUrl2 != "")
                    {
                        AddStr(p.PicUrl2, ref str1, ref str2);
                    }
                    if (p.PicUrl3 != "")
                    {
                        AddStr(p.PicUrl3, ref str1, ref str2);
                    }
                    if (p.PicUrl4 != "")
                    {
                        AddStr(p.PicUrl4, ref str1, ref str2);
                    }
                    if (p.PicUrl5 != "")
                    {
                        AddStr(p.PicUrl5, ref str1, ref str2);
                    }
                    if (p.PicUrl6 != "")
                    {
                        AddStr(p.PicUrl6, ref str1, ref str2);
                    }
                    str1 += "</ul>";
                    str2 += " </ul>";
                    fox.InnerHtml = str1 + str2;
                    tit.InnerHtml = p.Explain;
                    lblJF.InnerHtml = p.Integral.ToString();
                    lblXL.InnerHtml = "剩余" + p.REDP_INV.ToString() + "件";
                    xq.InnerHtml = p.Details.Replace("<p>","").Replace("</p>","");
                }

            }
            if (Request.QueryString["id"] != null && Request.QueryString["num"] != null) //加入购物车
            {
                if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
                {
                    string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
                    string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
                    PersonInfo per = fbo.GetModel(user);
                    if (per != null)
                    {
                        List<ShoppingCart> list = sbo.GetModelList(" pid=" + per.ID);
                        bool isok = true;
                        foreach (ShoppingCart car in list)
                        {
                            if (int.Parse(Request.QueryString["id"].ToString()) == car.RID)
                            {
                                isok = false;
                                car.RNum += int.Parse(Request.QueryString["num"].ToString());
                                sbo.Update(car);
                                AlertMessage("已加入购物车");
                            }
                        }
                        if (isok)
                        {
                            ShoppingCart s = new ShoppingCart();
                            s.PID = per.ID;
                            s.RID = int.Parse(Request.QueryString["id"].ToString());
                            s.RNum = int.Parse(Request.QueryString["num"].ToString());
                            int sid = sbo.Add(s);
                            if (sid > 0)
                            {
                                list = sbo.GetModelList(" pid=" + per.ID);
                                if (list.Count > 0)
                                {
                                    sumcar.InnerHtml = "<span><a href='ShopCar.aspx'><em class='indexicon4'></em><i>" + list.Count + "</i></a></span>";
                                }
                                addcar.InnerHtml = "<font color='red'>已加入购物车</font>";
                                AlertMessage("已加入购物车");
                            }
                        }
                    }
                    else
                    {
                        addcar.InnerHtml = "<font color='red'>请先注册和绑定</font>";
                        Response.Redirect("index2.aspx");
                    }

                }
                else
                {
                    addcar.InnerHtml = "<font color='red'>未获取到微信信息</font>";
                }
            }
        }
    }

    public void AlertMessage(string message)
    {
        Page.RegisterStartupScript("AlertMessage", "<script language='javascript'>alert(\"" + message.Replace("'", "′") + "\");</script>");
    }

    private static void AddStr(string url, ref string str1, ref string str2)
    {
        str1 += " <li class='nor'></li>";
        str2 += "<li><a href='#'><img src='" + url + "' width='320' height='320' /></a></li>";
    }
}